package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Owner;
import cn.qf.shop.pojo.qo.OwnerQo;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.PersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Resource;
import java.util.List;

@EnableSwagger2
@RequestMapping("/owner")
@Api(tags = "人员管理业务接口")
@RestController
@RestControllerAdvice
public class OwnerController extends BaseController{
private Logger logger= LoggerFactory.getLogger(OwnerController.class);
@Resource
private PersonnelService personnelManagement;
    /**
     * 查询所有人员信息列表，返回给前端页面
     * @return
     */
    @ResponseBody
@ApiOperation("人员管理数据展示")
   @RequestMapping(value = "/showManagement",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public RespResult<Owner> showManagement(){
    List<Owner> owners = personnelManagement.selectAll();
    RespResult<Owner> respResult=null;
    if (owners==null){
         respResult = super.returnRespWithFailed("找不到用户列表");
    }
   respResult= super.returnRespWithSuccess(owners);
    return respResult;
}

    /**
     * 根据搜索框搜索用户信息
     * @param ownerQo
     * @return
     */
    @ResponseBody
@ApiOperation("用户动态查询")
@RequestMapping(value = "/selectManagement",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public RespResult selectManagement(OwnerQo ownerQo){
        List<Owner> owners = personnelManagement.selectByKeyWord(ownerQo);
        RespResult<Owner> respResult=null;
    if (owners==null){
        respResult = super.returnRespWithFailed("找不到用户列表");

    }
    respResult= super.returnRespWithSuccess(owners);
    return respResult;
}

    /**
     * 根据用户勾选的id进行批量删除
     * @param ownerIds
     * @return
     */
    @ResponseBody
@ApiOperation("批量删除")
@RequestMapping(value = "/deleteManagement",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public RespResult<Owner> deleteManagement(@RequestParam(value = "ownerIds[]") List<Integer> ownerIds){
    int i = personnelManagement.updateByOwnerIds(ownerIds);
        RespResult<Owner>  respResult=null;
    if (i==-1){
        respResult = super.returnRespWithFailed("删除失败");
    }
    respResult=showManagement();

    return respResult;
}

    /**
     * 添加数据
     * @param ownerQo
     * @return
     */
    @ResponseBody
    @ApiOperation("增加数据")
    @RequestMapping(value = "/AddManagement",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public RespResult<Owner>  AddManagement(OwnerQo ownerQo){
        int i = personnelManagement.AddOwner(ownerQo);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("添加失败");
        }
        respResult=showManagement();
        return respResult;
    }

    /**
     * 根据点击鼠标来修改数据
     * @param ownerQo
     * @return
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/updateByOnclick",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public RespResult<Owner>  updateByOnclick(OwnerQo ownerQo){
        int i = personnelManagement.updateByOnclick(ownerQo);
        RespResult<Owner>  respResult=null;
        if (i==-1){
            respResult.setMessage("修改失败");
        }
        respResult=showManagement();

        return respResult;
    }
    /**
     * 根据点击图标删除所在的行
     * @param ownerQo
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteByid",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public RespResult<Owner>  deleteByid(OwnerQo ownerQo){
        int i = personnelManagement.updateByOwnerId(ownerQo);
        RespResult<Owner> respResult=null;
        if (i==-1){
            respResult.setMessage("删除失败");
        }
        respResult=showManagement();
        return respResult;
    }
}
